package com.nickstumpos.android.GmailWidgetPlus.widget;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.nickstumpos.android.GmailWidgetPlus.Constants.GMWP_Constants;
import com.nickstumpos.android.GmailWidgetPlus.service.GmailWidgetService;
import com.nickstumpos.android.GmailWidgetPlus.widget.sizes.WidgetSize;

public abstract class WidgetController extends BroadcastReceiver {

	public WidgetController() {
		super();
	}

	protected abstract WidgetSize getWidgetSize();

	@Override
	public void onReceive(final Context context, final Intent intent) {

		final String action = intent.getAction();
		final Bundle extras = intent.getExtras();
		if (extras != null) {
			if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
				if (extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_IDS)) {
					context.startService(new Intent(context,
							GmailWidgetService.class) {

						{
							this.putExtra(GMWP_Constants.EXTRA_WIDGET_ACTION,
									GMWP_Constants.WIDGET_SYSTEM_UPDATE);
							this.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
									extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS));
							this.putExtra(GMWP_Constants.EXTRA_WIDGET_SIZE,
									WidgetController.this.getWidgetSize()
											.toString());
						}
					});
				}
			} else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {} else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
				context.stopService(new Intent(context,
						GmailWidgetService.class));
				context.getSharedPreferences(GMWP_Constants.GMW_PREFS_REF, 0)
						.edit()
						.clear()
						.commit();
			} else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
				if (extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
					context.startService(new Intent(context,
							GmailWidgetService.class) {

						{
							this.putExtra(GMWP_Constants.EXTRA_WIDGET_ACTION,
									GMWP_Constants.WIDGET_RMV_WIDGET);
							this.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
									extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID));
							this.putExtra(GMWP_Constants.EXTRA_WIDGET_SIZE,
									WidgetController.this.getWidgetSize()
											.toString());
						}
					});
				}
			} else if ((this.getWidgetSize() != null) && (this.getWidgetSize()
					.getAction() != null)
					&& this.getWidgetSize().getAction().equals(action)) {
				context.startService(new Intent(context,
						GmailWidgetService.class) {

					{
						if (intent.hasExtra(GMWP_Constants.EXTRA_WIDGET_ACTION)) {
							this.putExtra(GMWP_Constants.EXTRA_WIDGET_ACTION,
									intent.getExtras()
											.getString(GMWP_Constants.EXTRA_WIDGET_ACTION));
						} else {
							this.putExtra(GMWP_Constants.EXTRA_WIDGET_ACTION,
									GMWP_Constants.WIDGET_GMAIL_UPDATE);
						}
						this.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
								extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID));
						this.putExtra(GMWP_Constants.EXTRA_WIDGET_SIZE,
								WidgetController.this.getWidgetSize()
										.toString());
					}
				});
			}
		}
	}

}
